from fastapi import APIRouter, Request

from pyJianYingDraft import CapCut_Intro_type, Intro_type, CapCut_Outro_type, Outro_type, CapCut_Group_animation_type, \
    Group_animation_type
from .base import RouterMeta
from src.constants.resultcode import ResultCode
from src.utils.response import ApiResponse
from pydantic import BaseModel, Field
from src.service.add_image import add_image_impl
import config
from src.utils.logger import get_logger
from src.schemas.image import DraftAddImageRequest


logger = get_logger("capcut")
router = APIRouter()




@router.get("/intro_anim_types",name="获取图片入场动画")
def get_intro_animation_types():
    """
    获取可用入场动画
    """
    try:
        animation_types = []
        if config.IS_CAPCUT_ENV:
            # Return entrance animation types in CapCut environment
            for name, member in CapCut_Intro_type.__members__.items():
                animation_types.append({
                    "intro_anim_type_name": name
                })
        else:
            # Return entrance animation types in JianYing environment
            for name, member in Intro_type.__members__.items():
                animation_types.append({
                    "intro_anim_type_name": name
                })
        return ApiResponse.success(
            data=animation_types
        )
    except Exception as e:
        logger.error(f"获取图片入场动画: {str(e)}", exc_info=True)
        return ApiResponse.error(
            result_code=ResultCode.ERROR,
            message=f"获取图片入场动画: {str(e)}",
        )

@router.get("/outro_anim_types",name="获取图片出场动画")
def get_outro_animation_types():
    """
    获取可用入场动画
    """
    try:
        animation_types = []
        if config.IS_CAPCUT_ENV:
            # Return entrance animation types in CapCut environment
            for name, member in CapCut_Outro_type.__members__.items():
                animation_types.append({
                    "outro_anim_type_name": name
                })
        else:
            # Return entrance animation types in JianYing environment
            for name, member in Outro_type.__members__.items():
                animation_types.append({
                    "outro_anim_type_name": name
                })
        return ApiResponse.success(
            data=animation_types
        )
    except Exception as e:
        logger.error(f"获取图片出场动画: {str(e)}", exc_info=True)
        return ApiResponse.error(
            result_code=ResultCode.ERROR,
            message=f"获取图片出场动画: {str(e)}",
        )

@router.get("/combo_anim_types",name="获取图片组合动画")
def get_combo_anim_types():
    """
    获取可用入场动画
    """
    try:
        animation_types = []
        if config.IS_CAPCUT_ENV:
            # Return entrance animation types in CapCut environment
            for name, member in CapCut_Group_animation_type.__members__.items():
                animation_types.append({
                    "combo_anim_type_name": name
                })
        else:
            # Return entrance animation types in JianYing environment
            for name, member in Group_animation_type.__members__.items():
                animation_types.append({
                    "combo_anim_type_name": name
                })
        return ApiResponse.success(
            data=animation_types
        )
    except Exception as e:
        logger.error(f"获取图片组合动画: {str(e)}", exc_info=True)
        return ApiResponse.error(
            result_code=ResultCode.ERROR,
            message=f"获取图片组合动画: {str(e)}",
        )


@router.post("/add_image",name="添加图片")
async def add_image_to_draft(request: Request, image_request: DraftAddImageRequest):
    """
    向草稿添加图片
    """
    try:
        draft_result = add_image_impl(
            draft_folder=image_request.draft_folder,
            image_url=image_request.image_url,
            width=image_request.width,
            height=image_request.height,
            start=image_request.start,
            end=image_request.end,
            draft_id=image_request.draft_id,
            transform_y=image_request.transform_y,
            scale_x=image_request.scale_x,
            scale_y=image_request.scale_y,
            transform_x=image_request.transform_x,
            track_name=image_request.track_name,
            relative_index=image_request.relative_index,  # Pass track rendering order index parameter
            animation=image_request.animation,  # Pass entrance animation parameter (backward compatibility)
            animation_duration=image_request.animation_duration,  # Pass entrance animation duration
            intro_animation=image_request.intro_animation,  # Pass new entrance animation parameter
            intro_animation_duration=image_request.intro_animation_duration,
            outro_animation=image_request.outro_animation,  # Pass exit animation parameter
            outro_animation_duration=image_request.outro_animation_duration,  # Pass exit animation duration
            combo_animation=image_request.combo_animation,  # Pass combo animation parameter
            combo_animation_duration=image_request.combo_animation_duration,  # Pass combo animation duration
            transition=image_request.transition,  # Pass transition type parameter
            transition_duration=image_request.transition_duration,  # Pass transition duration parameter (seconds)
            # Pass mask related parameters
            mask_type=image_request.mask_type,
            mask_center_x=image_request.mask_center_x,
            mask_center_y=image_request.mask_center_y,
            mask_size=image_request.mask_size,
            mask_rotation=image_request.mask_rotation,
            mask_feather=image_request.mask_feather,
            mask_invert=image_request.mask_invert,
            mask_rect_width=image_request.mask_rect_width,
            mask_round_corner=image_request.mask_round_corner,
            background_blur=image_request.background_blur
        )
        return ApiResponse.success(
            data=draft_result,
        )
    except Exception as e:
        return ApiResponse.error(
            result_code=ResultCode.ERROR,
            message=f"草稿添加图片失败: {str(e)}",
        )



# 路由元数据配置
meta = RouterMeta(
    router=router,
    prefix="/image",
    tags=["图片"]
)